<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
	</body>
	<script type="text/javascript">
		// 基本数据类型：按值传递。
		//浅拷贝： 改变copy后的值，原先的值也将会改变。
		obj1 = {
			name: 'fyr',
			age: 20,
			friend:{
				name: 'zmmm',
				age: 22,
				friend:{
					name:'xxxy'
				}
			}
		}
		function shadowCopy(toObj,fromObj){
			for(var key in fromObj){
				toObj[key] = fromObj[key]
			}
			return toObj;
		}
		var obj2 = shadowCopy({},obj1)
		// console.log(obj2);
		// obj2.friend.friend.name = 'alex';
		// console.log(obj2);
		// console.log(obj1);
		
		function deepCopy(toObj,fromObj){
			for(var key in fromObj){
				if(fromObj.hasOwnProperty(key)){
					if(fromObj[key] && typeof fromObj[key] === 'object'){
						toObj[key] = fromObj[key].constructor === Array ? [] : {};
						toObj[key] = deepCopy(toObj[key],fromObj[key])
					}else{
						toObj[key] = fromObj[key];
					}
				}
			}
			return toObj;
		}
		var obj3 = deepCopy({},obj1)
		console.log(obj3);
	</script>
</html>
